package com.shbaiche.bifenba.ui.match;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sevenheaven.segmentcontrol.SegmentControl;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.adapter.match.MatchListAdapter;
import com.shbaiche.bifenba.adapter.match.MatchOddsInfoListAdapter;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.data.CompeteVo;
import com.shbaiche.bifenba.model.match.MatchesListMessage;
import com.shbaiche.bifenba.model.match.MatchesVo;
import com.shbaiche.bifenba.model.match.OddsInfoListVo;
import com.shbaiche.bifenba.model.match.OddsInfoMessage;
import com.shbaiche.bifenba.model.match.OddsInfoVo;
import com.shbaiche.bifenba.model.match.OddsInfoVoBox;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.ui.base.BaseFragment;
import com.shbaiche.bifenba.ui.base.XStateFragmentAdapter;
import com.shbaiche.bifenba.ui.data.DataIntegralFragment;
import com.shbaiche.bifenba.ui.data.DataPlayerFragment;
import com.shbaiche.bifenba.ui.data.DataScheduleFragment;
import com.shbaiche.bifenba.ui.data.DataTeamFragment;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroid.base.SimpleRecAdapter;
import cn.droidlover.xrecyclerview.RecyclerItemCallback;
import cn.droidlover.xrecyclerview.XRecyclerContentLayout;
import cn.droidlover.xrecyclerview.XRecyclerView;
import okhttp3.Call;

/**
 * 比赛详情-赔率
 */

@SuppressLint("ValidFragment")
public class MatchDetailOddsFragment extends BaseFragment {

    @BindView(R.id.segment_control)
    SegmentControl segmentControl;

    @BindView(R.id.contentLayout)
    XRecyclerContentLayout contentLayout;

    @BindView(R.id.tv_odds_field_0)
    TextView tvfield_0;

    @BindView(R.id.tv_odds_field_1)
    TextView tvfield_1;

    @BindView(R.id.tv_odds_field_2)
    TextView tvfield_2;

    @BindView(R.id.tv_odds_field_3)
    TextView tvfield_3;

    @BindView(R.id.tv_odds_field_4)
    TextView tvfield_4;

    @BindView(R.id.tv_odds_field_5)
    TextView tvfield_5;

    @BindView(R.id.layout_odds_title)
    LinearLayout layouTitle;





    MatchOddsInfoListAdapter adapter;

    MatchesVo mMatchVo;

    List<OddsInfoListVo> voList;

    @SuppressLint("ValidFragment")
    public MatchDetailOddsFragment(){

    }

    public static MatchDetailOddsFragment newInstance() {
        return new MatchDetailOddsFragment();
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        if(savedInstanceState!=null && savedInstanceState.containsKey("data")){
            mMatchVo = (MatchesVo)savedInstanceState.getSerializable("data");
        }else{
            mMatchVo = (MatchesVo)getArguments().getSerializable("data");
        }

        initAdapter();
        loadData();


        segmentControl.setOnSegmentControlClickListener(new SegmentControl.OnSegmentControlClickListener() {
            @Override
            public void onSegmentControlClick(int index) {
                showContent(index);
            }
        });

    }

    private void showContent(int index){
        if(voList!=null && voList.size()>index){

            OddsInfoListVo vo = voList.get(index);
            if(vo!=null){

                if(vo.getFieldName()!=null && vo.getFieldName().length == 6){
                    tvfield_0.setText(vo.getFieldName()[0]);
                    tvfield_1.setText(vo.getFieldName()[1]);
                    tvfield_2.setText(vo.getFieldName()[2]);
                    tvfield_3.setText(vo.getFieldName()[3]);
                    tvfield_4.setText(vo.getFieldName()[4]);
                    tvfield_5.setText(vo.getFieldName()[5]);
                }
                layouTitle.setVisibility(View.VISIBLE);
                adapter.setData(vo.getBox());
            }



        }
    }

    private void initAdapter() {
        setLayoutManager(contentLayout.getRecyclerView());
        contentLayout.getRecyclerView()
                .setAdapter(getAdapter());
        contentLayout.getRecyclerView()
                .setOnRefreshAndLoadMoreListener(new XRecyclerView.OnRefreshAndLoadMoreListener() {
                    @Override
                    public void onRefresh() {
                        loadData();
                    }

                    @Override
                    public void onLoadMore(int page) {
                        loadData();
                    }
                });

        contentLayout.loadingView(View.inflate(getContext(), R.layout.view_loading, null));
        contentLayout.getRecyclerView().useDefLoadMoreView();
    }

    private void setViews(OddsInfoMessage message){
        if(message!=null && message.getVoList()!=null && message.getVoList().size()>0){
            voList = message.getVoList();
            String[] menus = new String[voList.size()];
            OddsInfoListVo vo;
            for (int i = 0 ; i< voList.size();i++){
                vo = voList.get(i);
                menus[i] = vo.getName();
            }
            segmentControl.setText(menus);
            segmentControl.setSelectedIndex(0);
            segmentControl.setVisibility(View.VISIBLE);
            showContent(0);

        }
    }

    public void loadData() {
        if(voList==null){
            contentLayout.showLoading();
        }

        OddsInfoMessage message = new OddsInfoMessage();
        message.setCompetId(mMatchVo.getCompeteId());
        message.setGameType(mMatchVo.getGameType());
        message.setGameId(mMatchVo.getGameId());
        NetApi.matchesOddsInfo(message, new JsonCallback<TransMessage>() {
            @Override
            public void onFail(Call call, Exception e, int id) {
                contentLayout.showContent();
                contentLayout.getSwipeRefreshLayout().setRefreshing(false);
                ToastUtil.show("onFail" + e.getMessage());
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                contentLayout.showContent();
                contentLayout.getSwipeRefreshLayout().setRefreshing(false);
                ToastUtil.show("onFail" + e.getMessage());

            }

            @Override
            public void onResponse(TransMessage response, int id) {
                contentLayout.showContent();
                contentLayout.getSwipeRefreshLayout().setRefreshing(false);
                OddsInfoMessage result =new Gson().fromJson(response.getMessage(),OddsInfoMessage.class);
                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, result.getCode())) {
                    if(result!=null && result.getVoList()!=null){
                        Log.e("t","onResponse");

                        setViews(result);

                    }
                }else{
                    Log.e("t","onResponse getCode "+response.getCode());
                    ToastUtil.show(result.getMessage());
                }
            }
        });





    }

    public SimpleRecAdapter getAdapter() {
        if (adapter == null) {
            adapter = new MatchOddsInfoListAdapter(getActivity());
            adapter.setRecItemClick(new RecyclerItemCallback<OddsInfoVoBox, MatchOddsInfoListAdapter.ViewHolder>() {
                @Override
                public void onItemClick(int position, OddsInfoVoBox model, int tag, MatchOddsInfoListAdapter.ViewHolder holder) {
                    super.onItemClick(position, model, tag, holder);
                }
            });
        }
        return adapter;
    }


    public void setLayoutManager(XRecyclerView recyclerView) {
        recyclerView.verticalLayoutManager(context);
    }






    @Override
    public int getLayoutId() {
        return R.layout.fragment_match_detail_odds;
    }
}
